package com.memeuxe.awtrix.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import com.memeuxe.awtrix.entity.Command;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CommandHttpSend {
    @Value("${awtrix.server.url}")
    private String awtrixServerUrl;

    public boolean send2Awtrix(Command command){
        //        curl --header "Content-Type: application/json" --request POST --data {"msgShort":"Hello World"} http://[AWTRIX-SERVER_IP]:7000/api/v3/basics
        log.info("发送={}", command);
        HttpResponse resp = HttpRequest.post(awtrixServerUrl + command.getType())
                .body(command.getCommandStr())
                .header("Content-Type", "application/json")
                .timeout(20000)
                .execute();
        int status = resp.getStatus();
        log.info("response.status={}", status);
        String body = resp.body();
        log.info("response.body={}", body);
        return status == HttpStatus.HTTP_OK;
    }
}
